﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;

namespace TaskKernel.Domain
{
    public static class DateTimeExtensions
    {
        #region RoundTo enum

        public enum RoundTo
        {
            Second,
            Minute,
            Hour,
            Day
        }

        #endregion

        public static DateTime ReplaceDate(this DateTime d, DateTime s)
        {
            return new DateTime(s.Year, s.Month, s.Day, d.Hour, d.Minute, d.Second);
        }

        public static DateTime TrimDate(this DateTime d)
        {
            return new DateTime(SqlDateTime.MinValue.Value.Year, SqlDateTime.MinValue.Value.Month,
                                SqlDateTime.MinValue.Value.Day,
                                d.Hour, d.Minute, d.Second);
        }

        public static DateTime Round(this DateTime d, RoundTo rt)
        {
            var dtRounded = new DateTime();
            switch (rt)
            {
                case RoundTo.Second:
                    dtRounded = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second);
                    if (d.Millisecond >= 500) dtRounded = dtRounded.AddSeconds(1);
                    break;
                case RoundTo.Minute:
                    dtRounded = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, 0);
                    break;
                case RoundTo.Hour:
                    dtRounded = new DateTime(d.Year, d.Month, d.Day, d.Hour, 0, 0);
                    if (d.Minute >= 30) dtRounded = dtRounded.AddHours(1);
                    break;
                case RoundTo.Day:
                    dtRounded = new DateTime(d.Year, d.Month, d.Day, 0, 0, 0);
                    if (d.Hour >= 12) dtRounded = dtRounded.AddDays(1);
                    break;
            }
            return dtRounded;
        }
    }

    public static class DomainModelsAccount
    {
        public static string StringFromList(this IEnumerable<Account> list)
        {
            string outStr = "";
            foreach (Account account in list)
                outStr += account.FullName + "; ";
            return outStr;
        }

        public static string ToStringOverSemicolon(this IEnumerable<Account> accounts)
        {
            string result = "";
            foreach (Account account in accounts)
            {
                if (account != null)
                    result += !String.IsNullOrEmpty(account.FullName) ? account.FullName + "; " : "";
            }
            return result;
        }

        public static void UpdateFromView(this Account changeAccount, Account accountView)
        {
            changeAccount.Login = accountView.Login;
            changeAccount.FullName = accountView.FullName;
            changeAccount.Mail = accountView.Mail;
            changeAccount.Fired = accountView.Fired;
            changeAccount.DepartmentName = accountView.DepartmentName;
            changeAccount.JobTitle = accountView.JobTitle;
            changeAccount.Phone = accountView.Phone;
        }
    }

    public static class DomainModelsExtensions
    {
        public static string ToStringComments(this IEnumerable<AbstractTaskHistoryEntry> entryList, string separator)
        {
            List<AbstractTaskHistoryEntry> list = entryList.ToList();
            return list.Aggregate("",
                                  (current, entry) =>
                                  current + (entry.DateCreate + ": " + entry.Comment + separator));
        }

        public static string ToStringFiles(this IEnumerable<AbstractTaskHistoryEntry> entryList, string separator)
        {
            List<AbstractTaskHistoryEntry> list = entryList.ToList();
            return list.Aggregate("",
                                  (current, entry) =>
                                  current + (entry.DateCreate + ": " + entry.File.FileName + separator));
        }
    }
}